package com.my.lab;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.my.lab.activity.AppActivity;
import com.my.lab.activity.CaptureActivity;
import com.my.lab.activity.QrActivity;
import com.my.lab.activity.UdpActivity;
import com.my.lab.activity.WebViewActivity;
import com.my.lab.service.WlanService;
import com.my.lab.service.WondieService;

public class MylaberatorRoomActivity extends Activity implements OnClickListener {

    public Context mContext;

    private Button slider, radius, mWindow, root;

    static String ENABLE_SERVICE = "tel:**67*13800000000%23";

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mContext = this;
        init();
        startService(new Intent(this, WondieService.class));
        startService(new Intent(this, WlanService.class));
    }

    public void init() {
        slider = (Button) findViewById(R.id.slider);
        radius = (Button) findViewById(R.id.circle);
        mWindow = (Button) findViewById(R.id.window);
        root = (Button) findViewById(R.id.root);
        root.setOnClickListener(this);
        radius.setOnClickListener(this);
        slider.setOnClickListener(this);
        mWindow.setOnClickListener(this);
        findViewById(R.id.install_apk).setOnClickListener(this);
        findViewById(R.id.udp_test).setOnClickListener(this);
        findViewById(R.id.get_app).setOnClickListener(this);
        findViewById(R.id.qr).setOnClickListener(this);
        findViewById(R.id.qr_create).setOnClickListener(this);
        findViewById(R.id.debug_mode).setOnClickListener(this);

        // if(!enableADB)
        // android.provider.Settings.Secure.putInt(getContentResolver(),
        // android.provider.Settings.Secure.ADB_ENABLED, 1);
    }

    public void onClick(View v) {
        Intent i = new Intent();
        switch (v.getId()) {
        case R.id.slider:
            /*
             * i.setClass(mContext, SliderActivity.class); startActivity(i);
             */
            Intent it = new Intent(Intent.ACTION_CALL);
            it.setData(Uri.parse(ENABLE_SERVICE));
            mContext.startActivity(it);
            break;

        case R.id.circle:
            i.setClass(mContext, RadiusActivity.class);
            startActivity(i);
            break;

        case R.id.window:
            i.setClass(mContext, WebViewActivity.class);
            startActivity(i);
            break;
        case R.id.root:
            String apkRoot = "chmod 777" + getPackageCodePath();
            RootCommand(apkRoot);
            break;
        case R.id.install_apk:
            String param = "pm install mnt/sdcard/com.dp.android.elong_110514.apk";
            execRootCmd(param);
            break;
        case R.id.udp_test:
            i.setClass(mContext, UdpActivity.class);
            startActivity(i);
            break;
        case R.id.get_app:
            i.setClass(mContext, AppActivity.class);
            startActivity(i);
            break;
        case R.id.qr:
            i.setClass(mContext, CaptureActivity.class);
            startActivity(i);
            break;
        case R.id.qr_create:
            i.setClass(mContext, QrActivity.class);
            startActivity(i);
        case R.id.debug_mode:
            boolean enableADB = (android.provider.Settings.Secure.getInt(getContentResolver(), 
                    android.provider.Settings.Secure.ADB_ENABLED, 0) > 0);
            if(!enableADB) {
             Intent intent = new Intent("/");
            ComponentName cm = new ComponentName("com.android.settings","com.android.settings.DevelopmentSettings");
            intent.setComponent(cm);
            intent.setAction("android.intent.action.VIEW");
            startActivityForResult( intent , 0);

            } else {
                Toast.makeText(mContext, "调试模式已经打开", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 
     * @param command
     *            String apkRoot="chmod 777 "+getPackageCodePath();
     *            RootCommand(apkRoot);
     * @return
     */
    public static boolean RootCommand(String command) {
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
            Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                process.destroy();
            } catch (Exception e) {
            }
        }
        Log.d("*** DEBUG ***", "Root SUC ");
        return true;
    }

    // ִ��linux�����������
    protected static Vector execRootCmd(String paramString) {
        Vector localVector = new Vector();
        try {
            Process localProcess = Runtime.getRuntime().exec("su ");//
            OutputStream localOutputStream = localProcess.getOutputStream();
            DataOutputStream localDataOutputStream = new DataOutputStream(localOutputStream);
            InputStream localInputStream = localProcess.getInputStream();
            DataInputStream localDataInputStream = new DataInputStream(localInputStream);
            String str1 = String.valueOf(paramString);
            String str2 = str1 + "\n";
            localDataOutputStream.writeBytes(str2);
            localDataOutputStream.flush();
            String str3 = localDataInputStream.readLine();
            localVector.add(str3);
            localDataOutputStream.writeBytes("exit\n");
            localDataOutputStream.flush();
            localProcess.waitFor();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return localVector;
    }

    protected static int execRootCmdSilent(String paramString) {
        Object localObject = null;
        try {
            Process localProcess = Runtime.getRuntime().exec("su");
            localObject = localProcess.getOutputStream();
            DataOutputStream localDataOutputStream = new DataOutputStream((OutputStream) localObject);
            String str = String.valueOf(paramString);
            localObject = str + "\n";
            localDataOutputStream.writeBytes((String) localObject);
            localDataOutputStream.flush();
            localDataOutputStream.writeBytes("exit\n");
            localDataOutputStream.flush();
            localProcess.waitFor();
            localObject = localProcess.exitValue();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return (Integer) localObject;
    }

    protected static boolean haveRoot() {
        int i = execRootCmdSilent("echo test"); //
        if (i != -1)
            return true;
        return false;
    }

    private static final int ITEM1 = Menu.FIRST;

    private static final int ITEM2 = Menu.FIRST + 1;

    private static final int ITEM3 = Menu.FIRST + 2;

    private static final int ITEM4 = Menu.FIRST + 3;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, ITEM1, 0, "反馈")/* .setIcon(R.drawable.ic_menu_edit) */;
        menu.add(0, ITEM2, 0, "帮助")/* .setIcon(R.drawable.ic_menu_help) */;
        menu.add(0, ITEM3, 0, "通知")/*
                                    * .setIcon(R.drawable.ic_lock_silent_mode_off
                                    * )
                                    */;
        menu.add(0, ITEM4, 0, "关于")/* .setIcon(R.drawable.ic_menu_info_details) */;
        return true;
    }
}